package com.example.homework7;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.example.homework7.Config;
import com.example.homework7.R;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * 外部存储Fragment 单例模式
 */
public class StorageExternalFragment extends Fragment {
    private static final String TAG = "StorageExternalFragment";
    private static StorageExternalFragment fragment;

    private String FILE_NAME = Config.EXTERNAL_FILE_NAME;

    private Button mBtnRead;
    private Button mBtnWrite;

    private StorageExternalFragment() {
    }

    /**
     * 获取单例
     * @return
     */
    public static StorageExternalFragment newInstance() {
        if(fragment == null){
            fragment = new StorageExternalFragment();
        }
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_storage_external, container, false);

        initView(view);
        initEvent();

        return view;
    }

    private void initEvent() {
        mBtnWrite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: 进行写入操作");
                File externalFilesDir = getActivity().getExternalFilesDir(null);
                File file = new File(externalFilesDir, FILE_NAME);
                try {
                    FileOutputStream os = new FileOutputStream(file);
                    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os));
                    writer.write(Config.EXTERNAL_WRITE_CONTENT);//写入的内容
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Toast.makeText(getActivity(), "写入成功", Toast.LENGTH_SHORT).show();
                Log.d(TAG, "onClick: 写入成功");
            }
        });

        mBtnRead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: 进行读取操作");
                File externalFilesDir = getActivity().getExternalFilesDir(null);
                File file = new File(externalFilesDir, FILE_NAME);
                try {
                    FileInputStream is = new FileInputStream(file);
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                    String line = reader.readLine();
                    Toast.makeText(getActivity(), "读取成功,内容是:" + line, Toast.LENGTH_SHORT).show();
                    is.close();
                    Log.d(TAG, "onClick: 读取成功,内容是: " + line);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    private void initView(View view) {
        mBtnRead = view.findViewById(R.id.btn_storage_external_read);
        mBtnWrite = view.findViewById(R.id.btn_storage_external_write);
    }
}